### PT run ###

if {$METHOD == "sta"} {
		set TOP_DIR $sh_launch_dir
		source -e -v ${TOP_DIR}/scripts/block_setup.tcl
		source -e -v ${TOP_DIR}/scripts/lib_setup.tcl
		source -e -v ${TOP_DIR}/scripts/pt_setup.tcl
}

### read design
if {$HIER_DESIGN == "true" && $SUB_BLOCK_1 != ""} {
		puts "==INFO: Hierarchical sta, read sub-block netlist"
		if {[file exist ${SUB_BLOCK_1_INPUT_PATH}]} {
				set BLOCK_1_NETLIST "${SUB_BLOCK_1_INPUT_PATH}/${SUB_BLOCK_1}.v"
				read_verilog ${BLOCK_1_NETLIST}
		} else {
				puts "ERROR: ${BLOCK_1_NETLIST} is not exist !!!"
				exit
		}
}

if {$HIER_DESIGN == "true" && $SUB_BLOCK_2 != ""} {
		puts "==INFO: Hierarchical sta, read sub-block netlist"
		if {[file exist ${SUB_BLOCK_2_INPUT_PATH}]} {
				set BLOCK_2_NETLIST "${SUB_BLOCK_2_INPUT_PATH}/${SUB_BLOCK_2}.v"
				read_verilog ${BLOCK_2_NETLIST}
		} else {
				puts "ERROR: ${BLOCK_2_NETLIST} is not exist !!!"
				exit
		}
}



if {[file exist ${NETLIST}] } {
		read_verilog ${NETLIST}
} else {
		puts "ERROR: ${INPUT_PATH}/${DESIGN}.v is not exist !!!"
		exit
}

current_design ${DESIGN}
link

### load spef
if {$HIER_DESIGN == "true" && $SUB_BLOCK_1 != ""} {
		puts "==INFO: Hierarchical sta, read sub-block netlist"
		if {[file exist $SUB_BLOCK_1_starrc_path]} {
				set BLOCK_1_SPEF "$SUB_BLOCK_1_parasitic_files($corner)"
				read_parasitics -keep_capacitive_coupling -format spef $BLOCK_1_SPEF -path [all_instances -hierarchy $SUB_BLOCK_1]
		} else {
				puts "ERROR: $BLOCK_1_SPEF is not exist !!!"
				exit
		}
}

if {$HIER_DESIGN == "true" && $SUB_BLOCK_2 != ""} {
		puts "==INFO: Hierarchical sta, read sub-block netlist"
		if {[file exist $SUB_BLOCK_2_starrc_path]} {
				set BLOCK_2_SPEF "$SUB_BLOCK_2_parasitic_files($corner)"
				read_parasitics -keep_capacitive_coupling -format spef $BLOCK_2_SPEF -path [all_instances -hierarchy $SUB_BLOCK_2]
		} else {
				puts "ERROR: $BLOCK_2_SPEF is not exist !!!"
				exit
		}
}


if { [info exist parasitic_files($corner)] && [file exist $parasitic_files($corner)] } {
		if {$SI_ENABLE} {
				puts "== INFO: SI enabled !"
				read_parasitics -keep_capacitive_coupling -format spef $parasitic_files($corner)
		} else {
				puts "== INFO: SI not enabled !"
				read_parasitics -format spef $parasitic_files($corner)
		}
} else {
		puts "ERROR: input dir ${corner} spef is not exist !!!"
		exit
}

### load constrain
if {[file exists $constrain]} {
		puts "== INFO: Loading constrain file $constrain ."
		source -e -v $constrain
} else {
		puts "ERROR: ${INPUT_PATH}/${DESIGN}.sdc not exist !!!"
		exit
}

set_clock_uncertainty -setup 0.10 [all_clocks]
set_clock_uncertainty -hold  0.10 [all_clocks]

### derate setting ###
proc set_derate_worst_case {} {
	set_timing_derate -clock -data -early 0.95 -cell_delay
	set_timing_derate -clock -data -early 0.95 -net_delay
	set_timing_derate -clock -data -late  1.12 -cell_delay
	set_timing_derate -clock -data -late  1.12 -net_delay

}
proc set_derate_best_case {} {
	set_timing_derate -clock -data -early 0.95 -cell_delay
	set_timing_derate -clock -data -early 0.95 -net_delay
	set_timing_derate -clock -data -late  1.12 -cell_delay
	set_timing_derate -clock -data -late  1.12 -net_delay
}
proc set_derate_typical_case {} {
	set_timing_derate -clock -data -early 0.95 -cell_delay
	set_timing_derate -clock -data -early 0.95 -net_delay
	set_timing_derate -clock -data -late  1.12 -cell_delay
	set_timing_derate -clock -data -late  1.12 -net_delay
}

proc set_ocv_all {corner} {
	if {[string match "wc*" $corner]} {
		set_derate_worst_case
	} elseif {[string match "ml*" $corner] || [string match "lt*" $corner] || [string match "bc*" $corner]} {
		set_derate_best_case
	} elseif {$corner=="tc25"} {
		set_derate_typical_case
	}
}

set_ocv_all $corner

set_propagated_clock [all_clocks]

update_timing


### sta run result.
if {![file exists session]} {
		sh mkdir -p session
}
if {![info exist METHOD] || ($METHOD != "dmsa")} {
		if {$SESSION_SAVE} {
				save_session ${TOP_DIR}/session/${corner}
		}
}

### reports generation
if {![file exists reports]} {
		sh mkdir -p reports
}
source -e -v ${TOP_DIR}/scripts/report.tcl

### move log file to logs dir.
if {[file exists parasitcs_command.log]} {
#file copy -force parasitcs_command.log logs/parasitcs_command.${corner}.log
	file delete parasitcs_command.log
}

if {[file exists pt_shell_command.log]} {
#file copy -force pt_shell_command.log logs/shell_command.${corner}.log
	file delete pt_shell_command.log
}

### write sdf
if {![file exists outputs]} {
		sh mkdir -p outputs
}
if {$SDF} {
		write_sdf -version 3.0 -no_edge -context verilog -exclude no_condelse -significant_digits 6 ./outputs/${DESIGN}_${corner}.sdf
}

### report power
if {$POWER} {
		set power_enable_analysis true
		if {[file exists saif]} {
				update_timing
				update_power
				report_power -hierarchy -verbose > $REPORTS_DIR/${corner}.static.power
				read_saif $saif -strip_path sim/top
				set_noise_parameter -include_beyond_rails -enable_propagation -analysis_mode report_at_endpoint
				update_timing
				update_power
				report_power -hierarchy -verbose > $REPORTS_DIR/${corner}.dynamic.power
		} else {
				update_timing
				update_power
				report_power -hierarchy -verbose > $REPORTS_DIR/${corner}.static.power
		}
}



### quit_shell
if {$SHELL_QUIT} {
		exit
}




